//
//  ScheduleViewController.m
//  Japanese+
//
//  Created by Jame on 15/10/20.
//  Copyright © 2015年 SSIC. All rights reserved.
//

#import "ScheduleViewController.h"
#import "ScheduleCell.h"
#import "ScheduleModel.h"

#import "ScheduleDetailViewController.h"

@interface ScheduleViewController () <CLWeeklyCalendarViewDelegate,UICollectionViewDataSource,UICollectionViewDelegate>

@end

@implementation ScheduleViewController
{
    CLWeeklyCalendarView *_calander;
    UICollectionView *_collectionView;
    
    MemberModel *_memberModel;
    TeacherModel *_teacherModel;

    ScheduleModel *_scheduleModel;
    NSMutableArray *_dataArray;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self creatNavigationBarWithImage:nil title:@"课表"];
    if (self.isStudent) {
        [self creatNavigationBarLeftItemWithLeftTitle:nil LeftImage:ECIMAGENAME(@"back_button.png")];
    }

    [self creatCalanderView];
    [self creatCollectionView];
}

- (void)viewWillAppear:(BOOL)animated
{
    if (!self.isStudent) {
        [self loadUserInfo];
    }
}


- (void)leftBtnClick:(id)leftSender
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)loadUserInfo
{
    UserInfoManager *userInfo = [UserInfoManager sharedUserInfoManager];
    [userInfo loadUserInfoFromSanbox];
    _memberModel = userInfo.memberModel;
    _teacherModel = userInfo.teacherModel;
}


- (void)creatCalanderView
{
    _calander = [[CLWeeklyCalendarView alloc] initWithFrame:CGRectMake(0, 64, ECSCREEN_W, 120)];
    _calander.delegate = self;
    [self.view addSubview:_calander];
}


-(NSDictionary *)CLCalendarBehaviorAttributes
{
    return @{
             CLCalendarWeekStartDay : @1,                 //Start Day of the week, from 1-7 Mon-Sun -- default 1
             CLCalendarDayTitleTextColor:[UIColor blackColor],
             CLCalendarSelectedDatePrintColor:[UIColor blackColor],
             };
}


-(void)dailyCalendarViewDidSelect:(NSDate *)date
{
    //You can do any logic after the view select the date
    NSLog(@"******%@",date);
    [self creatHttpRequestWithDate:date];
}


-  (void)creatHttpRequestWithDate:(NSDate *)date
{
    
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd"];
    NSString *dateString = [dateFormatter stringFromDate:date];
    NSLog(@"***PARAM***%@",dateString);
    NSDictionary *param = @{}.mutableCopy;
    if (self.isStudent) {
        param = @{
                  @"t_member_Id":self.memberId,
                  @"time":dateString,
                  };
    }else{
        param = @{
                                @"t_member_Id":_memberModel.member_Id,
                                @"time":dateString,
                                };
    }
    
    [[HttpManager defaultManager] postRequestToUrl:RequestURL(SCHEDULE) params:param complete:^(BOOL successed, NSDictionary *result) {
        if (successed) {
            [ScheduleModel setupObjectClassInArray:^NSDictionary *{
                return @{
                         @"data":@"ScheduleData",
                         };
            }];
            
            if (_dataArray) {
                [_dataArray removeAllObjects];
            }else{
                _dataArray = [[NSMutableArray alloc] init];
            }

            _scheduleModel = [[ScheduleModel alloc] init];
            _scheduleModel = [ScheduleModel objectWithKeyValues:result];
            [_dataArray addObjectsFromArray:_scheduleModel.data];
            [_collectionView reloadData];
        }
    }];
}

#pragma mark - creatCollectionView
- (void)creatCollectionView
{
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    
    layout.scrollDirection = UICollectionViewScrollDirectionVertical;
    CGFloat w = (ECSCREEN_W - 20)/3;
    layout.itemSize = CGSizeMake(w, w);
    layout.minimumInteritemSpacing = 5;
    layout.minimumLineSpacing = 5;
    layout.sectionInset = UIEdgeInsetsMake(5, 5, 5, 5);
    
    if (self.isStudent) {
        _collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 64 + 120, ECSCREEN_W, ECSCREEN_H - 64 - 120) collectionViewLayout:layout];
    }else{
        _collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 64 + 120, ECSCREEN_W, ECSCREEN_H - 49 - 64 - 120) collectionViewLayout:layout];
    }
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, ECSCREEN_W, ECSCREEN_H - 49 - 64)];
    imageView.backgroundColor = ECCOLOR(255, 255, 255, 1);
    _collectionView.showsVerticalScrollIndicator = NO;
    [_collectionView setBackgroundView:imageView];
    
    _collectionView.delegate = self;
    _collectionView.dataSource = self;
    
    [_collectionView registerNib:[UINib nibWithNibName:@"ScheduleCell" bundle:nil] forCellWithReuseIdentifier:@"scheduleCell"];
    
    [self.view addSubview:_collectionView];
}


#pragma mark - numOfSections
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}


#pragma mark - numOfItems
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return _dataArray.count;
}


#pragma mark - cellForItemIndexPath
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *reuseIdentifier = @"scheduleCell";
    ScheduleCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath];
    
    cell.backgroundColor = BG_COLOR;
    cell.clipsToBounds = YES;
    cell.layer.cornerRadius = 8;
    [cell setCellWithModel:_dataArray WithIndexPath:indexPath];
    return cell;
}


- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    ScheduleData *data = _dataArray[indexPath.row];
    ScheduleDetailViewController *vc = [[ScheduleDetailViewController alloc] init];
    vc.course_Id = data.course_Id;
    vc.isStudent = self.isStudent;
    vc.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:vc animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
